// 开发文档: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
const db = uniCloud.database();
const configDB = db.collection('sy-config');
const bannerDB = db.collection('sy-banner')
const downloadDB = db.collection('sy-download')
module.exports = {
	async getConfig() {
		return await configDB.get()
	},
	async getBanner() {
		return await bannerDB.orderBy("sort","asc").where({"status":true}).get()
	},
	async getDownload() {
		return await downloadDB.orderBy("sort","asc").where({"status":true}).get()
	},
	async updateConfig(data) {
		// console.log("data: ",data);
		let getData = await configDB.doc(data._id).get()
		// console.log("getData: ",getData);
		if (!getData.data || getData.data.length === 0) {
			return {
				msg: '未初始化数据，请先初始化db_init.json'
			}
		}
		// console.log("data._id: ",data._id);
		switch (data._id){
			case "header":
				return await configDB.doc(data._id).update({
					logo:data.logo,
					name:data.name,
					nameColor:data.nameColor,
					tabbar:data.tabbar,
					tabColor:data.tabColor,
					tabActiveColor:data.tabActiveColor,
					headerBackground:data.headerBackground,
					titleColor:data.titleColor,
					isShow:data.isShow
				})
				break;
			case "appIntroduce":
				return await configDB.doc(data._id).update({
					slogan:data.slogan,
					content:data.content,
					showImg:data.showImg
				})
				break;
			case "comIntroduce":
				return await configDB.doc(data._id).update({
					slogan:data.slogan,
					content:data.content
				})
				break;
			case "aboutus":
				return await configDB.doc(data._id).update({
					mobile:data.mobile,
					email:data.email,
					address:data.address,
					beianhao:data.beianhao,
					beianhaoUrl:data.beianhaoUrl,
					beiandi:data.beiandi,
					beiandiUrl:data.beiandiUrl,
					copyright:data.copyright,
					footColor:data.footColor
				})
				break;
			default:
				break;
		}
		
	}
}
